﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using LynxWiki.Filters;
using LynxWiki.DomainModel;
using LynxWiki.Search;
using LynxWiki.ViewModels;
using LynxWiki.Repository;
using DiffPlex;
using DiffPlex.DiffBuilder;
using DiffPlex.DiffBuilder.Model;


namespace LynxWiki.Controllers
{
    [InitializeSimpleMembership]
    public class WikiTopicController : Controller
    {
        //
        // GET: /WikiTopic/

        [TopicActionFilter]
        public ActionResult Index(string topicName = "HomePage")
        {

            ViewBag.Title = "Index";
            ViewBag.PagePath = "";
            ViewBag.TabbedMenuList = BuildMenuTabs();
            TopicVersionViewData tvd = new TopicVersionViewData();
            tvd.TopicName = topicName;
            return View(tvd);
        }

        [TopicActionFilter]
        public ActionResult Page(string pagePath)
        {
            ViewBag.Title = "QuickLink";
            ViewBag.PagePath = pagePath;
            //HttpContext.Session["__MyWorkingDir"] = new WorkingDirectory(Path.Combine(WikiRoot.RootDirectory, pagePath));
            HttpContext.Session["__MyWorkingDir"] = new WorkingDirectory(pagePath.Substring(0, pagePath.LastIndexOf("/")));
            TopicVersionViewData tvd = new TopicVersionViewData();
            tvd.TopicName = pagePath.Substring(pagePath.LastIndexOf("/") + 1);
            WikiTopicRepository wtr = new WikiTopicRepository(Server.MapPath(""));
            WikiDirectory wd = wtr.GetWikiDirectoryInfo(Server.MapPath(""));
            if (wtr.GetTopicVersionCount(tvd.TopicName) > 0) {
                ViewBag.TabbedMenuList = BuildMenuTabs(pagePath);
                tvd.Topic = wtr.GetCurrentVersion(tvd.TopicName);
                tvd.TopicLock = wtr.TopicIsLocked(tvd.TopicName + "__current.xml");
                tvd.AllowUpdate = tvd.Topic.UserWriteAccess(User);
                tvd.Topic.WikiDir = wtr.WikiDir;
                if (!DenyRead(tvd.Topic)) {
                    return View("Index", tvd);
                }
                else {
                    return RedirectToRoute("ShowAccessDenied", new { action = "ShowAccessDenied", controller = "WikiTopic", topicName = tvd.TopicName });
                }
            }
            else {
                return RedirectToRoute("PageDisplay", new { action = "Edit", controller = "WikiTopic", pagePath });
            }
        }

        [HttpPost]
        [TopicActionFilter]
        public ActionResult Page(FormCollection fc)
        {
            ViewBag.Title = "QuickLink";
            string pagePath = fc["PagePath"];
            if (!string.IsNullOrEmpty(pagePath)) {
                pagePath = pagePath.Substring(0, pagePath.LastIndexOf("/") + 1) + fc["TopicBarInputBox"];
                HttpContext.Session["__MyWorkingDir"] = new WorkingDirectory(Path.Combine(WikiRoot.RootDirectory, pagePath));
            }
            else {
                pagePath = this.Request.FilePath.Substring(this.Request.FilePath.IndexOf("/Page/") + 6);
                HttpContext.Session["__MyWorkingDir"] = new WorkingDirectory(Path.Combine(WikiRoot.RootDirectory, pagePath));
                TopicVersionViewData tvd = new TopicVersionViewData();
                tvd.TopicName = pagePath.Substring(pagePath.LastIndexOf("/") + 1);
                WikiTopicRepository wtr = new WikiTopicRepository(Server.MapPath(""));
                WikiDirectory wd = wtr.GetWikiDirectoryInfo(Server.MapPath(""));
                if (wtr.GetTopicVersionCount(tvd.TopicName) > 0) {
                    ViewBag.TabbedMenuList = BuildMenuTabs(pagePath);
                    tvd.Topic = wtr.GetCurrentVersion(tvd.TopicName);
                    tvd.TopicLock = wtr.TopicIsLocked(tvd.TopicName + "__current.xml");
                    tvd.AllowUpdate = tvd.Topic.UserWriteAccess(User);
                    tvd.Topic.WikiDir = wtr.WikiDir;
                    //tvd.FormPostRequest = this.Request;
                    if (!DenyRead(tvd.Topic)) {
                        return View("Index", tvd);
                    }
                    else {
                        return RedirectToRoute("ShowAccessDenied", new { action = "ShowAccessDenied", controller = "WikiTopic", topicName = tvd.TopicName });
                    }
                }
            }
            return RedirectToRoute("PageDisplay", new { action = "Page", controller = "WikiTopic", pagePath });
        }

        [TopicActionFilter]
        public ActionResult History(string pagePath)
        {
            try {
                ViewBag.Title = "QuickLink";
                ViewBag.PagePath = pagePath;
                ViewBag.User = User.Identity.Name;
                TopicVersionViewData tvd = new TopicVersionViewData();
                tvd.TopicName = pagePath.Substring(pagePath.LastIndexOf("/") + 1);
                tvd.PagePath = pagePath;
                ViewBag.TabbedMenuList = BuildMenuTabs(pagePath);
                WikiTopicRepository wtr = new WikiTopicRepository(Server.MapPath("~\\" + tvd.PagePath.Substring(0, tvd.PagePath.LastIndexOf("/"))));
                List<WikiTopicHistoryListingViewData> wthlvd = new List<WikiTopicHistoryListingViewData>();
                IEnumerable<TopicVersion> tvl = wtr.GetTopicVersions(tvd.TopicName);
                foreach (TopicVersion tv in tvl) {
                    WikiTopicHistoryListingViewData tvh = new WikiTopicHistoryListingViewData();
                    tvh.TopicName = tv.TopicName;
                    tvh.WikiDir = pagePath;
                    tvh.CreatedOn = tv.CreatedOn;
                    tvh.Creator = tv.Creator;
                    tvh.Version = tv.Version;
                    tvh.TopicBody = tv.Body;
                    wthlvd.Add(tvh);
                }
                return View(wthlvd);
            }
            catch {
                return View();
            }
        }

        //
        // GET: /WikiTopic/Edit/5

        [TopicActionFilter]
        public ActionResult Edit(string pagePath)
        {
            ViewBag.CaptchaSetting = (ConfigurationManager.AppSettings["CaptchaRequiredOnEdit"] == "true" ? "true" : "false");
            ViewBag.Title = "QuickLink";
            ViewBag.PagePath = pagePath;
            ViewBag.User = (string.IsNullOrEmpty(User.Identity.Name) ? "Anonymous" : User.Identity.Name);
            TopicVersionViewData tvd = new TopicVersionViewData();
            tvd.TopicName = pagePath.Substring(pagePath.LastIndexOf("/") + 1);
            tvd.PagePath = pagePath;
            WikiTopicRepository wtr = new WikiTopicRepository(Server.MapPath("~\\" + pagePath.Substring(0, pagePath.LastIndexOf("/"))));
            WikiDirectory wd = wtr.GetWikiDirectoryInfo(Server.MapPath(pagePath.Substring(0, pagePath.LastIndexOf("/") + 1)));
            if (wtr.GetTopicVersionCount(tvd.TopicName) > 0) {
                tvd.Topic = wtr.GetCurrentVersion(tvd.TopicName);
                tvd.AllowUpdate = tvd.Topic.UserWriteAccess(User);
                tvd.TopicLock = wtr.TopicIsLocked(tvd.TopicName + "__current.xml");
                ViewBag.TabbedMenuList = BuildMenuTabs(pagePath);
                return View("Edit", tvd);
            }
            else {
                // provide default content
                tvd.Topic = new TopicVersion();
                tvd.Topic.WikiText = @"!!Level 2 Header for a New Page

Edit the content of this message to create your new page. Add tags as required and then save the page.";
                tvd.Topic.Version = 0;
                tvd.AllowUpdate = true;
                ViewBag.TabbedMenuList = BuildMenuTabs(pagePath);
                return View("Edit", tvd);
            }
        }

        //
        // POST: /WikiTopic/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        [ActionName("EditTopic")]
        [AcceptSubmitButton(Name = "btnNewVersion", Value = "New Version")]
        [TopicActionFilter]
        [MaliciousActivityChecksFilter]
        [HandleError(ExceptionType = typeof(UnauthorizedAccessException), View = "AccessRestricted")]
        public ActionResult btnNewVersion(TopicVersionViewData tvd)
        {
            try {
                if (ConfigurationManager.AppSettings["CaptchaRequiredOnEdit"] == "true") {
                    if (Session["Captcha"] == null || Session["Captcha"].ToString() != tvd.CaptchaInput) {
                        ViewBag.CaptchaSetting = "true";
                        ModelState.AddModelError("CaptchaInput", "Wrong value entered, please try again.");
                        ViewBag.Title = "QuickLink";
                        ViewBag.PagePath = tvd.PagePath;
                        ViewBag.User = User.Identity.Name;
                        ViewBag.TabbedMenuList = BuildMenuTabs(tvd.PagePath);
                        return View("Edit", tvd);
                    }
                }
                try {

                    WikiTopicRepository wtr = new WikiTopicRepository(Server.MapPath("~\\" + tvd.PagePath.Substring(0, tvd.PagePath.LastIndexOf("/"))));
                    string userName = User.Identity.Name;
                    if (string.IsNullOrEmpty(userName)) {
                        userName = Request.UserHostAddress;
                    }
                    TopicVersion tv = new TopicVersion(tvd.TopicName, wtr.WikiDir, tvd.Topic.WikiText, userName,
                        ++tvd.Topic.Version, tvd.Topic.Body, tvd.ListDenyReadUser, tvd.ListDenyReadRole, tvd.ListAllowEditUser, tvd.ListAllowEditRole, tvd.Tags, tvd.Topic.EditReason);
                    tvd.Topic = tv;
                    wtr.Add(tv);
                    return RedirectToRoute("PageDisplay", new { action = "Page", controller = "WikiTopic", pagePath = tvd.PagePath });
                }
                catch {
                    return RedirectToRoute("ShowAccessDenied", new { action = "ShowAccessDenied", controller = "WikiTopic", topicName = tvd.TopicName });
                }
            }
            catch {
                return RedirectToRoute("ShowAccessDenied", new { action = "ShowAccessDenied", controller = "WikiTopic", topicName = tvd.TopicName });
            }
        }

        [HttpPost]
        [ValidateInput(false)]
        [ActionName("EditTopic")]
        [AcceptSubmitButton(Name = "btnMinorUpdate", Value = "Minor Update")]
        [TopicActionFilter]
        [MaliciousActivityChecksFilter]
        [HandleError(ExceptionType = typeof(UnauthorizedAccessException), View = "AccessRestricted")]
        public ActionResult btnMinorUpdate(TopicVersionViewData tvd)
        {
            if (ConfigurationManager.AppSettings["CaptchaRequiredOnEdit"] == "true") {
                if (Session["Captcha"] == null || Session["Captcha"].ToString() != tvd.CaptchaInput) {
                    ViewBag.CaptchaSetting = "true";
                    ModelState.AddModelError("CaptchaInput", "Wrong value entered, please try again.");
                    ViewBag.Title = "QuickLink";
                    ViewBag.PagePath = tvd.PagePath;
                    ViewBag.User = User.Identity.Name;
                    ViewBag.TabbedMenuList = BuildMenuTabs(tvd.PagePath);
                    return View("Edit", tvd);
                }
            }
            try {

                string userName = User.Identity.Name;
                if (string.IsNullOrEmpty(userName)) {
                    userName = Request.UserHostAddress;
                }
                WikiTopicRepository wtr = new WikiTopicRepository(Server.MapPath("~\\" + tvd.PagePath.Substring(0, tvd.PagePath.LastIndexOf("/"))));
                TopicVersion tv = new TopicVersion(tvd.TopicName, wtr.WikiDir, tvd.Topic.WikiText, userName,
                    tvd.Topic.Version, tvd.Topic.Body, String.Empty, string.Empty, string.Empty, string.Empty,
                    tvd.Tags, String.Empty, 
                    tvd.CreateListFromMinorUpdateDetails(), User.Identity.Name, tvd.MinorUpdate);
                tvd.Topic = tv;
                wtr.Update(tv);
                return RedirectToRoute("PageDisplay", new { action = "Page", controller = "WikiTopic", pagePath = tvd.PagePath });
            }
            catch {
                return RedirectToRoute("ShowAccessDenied", new { action = "ShowAccessDenied", controller = "WikiTopic", topicName = tvd.TopicName });
            }
        }

        [HttpPost]
        [ValidateInput(false)]
        [ActionName("EditTopic")]
        [AcceptSubmitButton(Name = "btnPreview", Value = "Preview")]
        [TopicActionFilter]
        public ActionResult btnPreview(TopicVersionViewData tvd)
        {
            try {
                // TODO: Add update logic here
                this.Session.Remove("__MyPreviewText");
                this.Session.Add("__MyPreviewText", tvd.Topic.HtmlText);
                return RedirectToAction("ShowPreview");
            }
            catch {
                return View();
            }
        }

        [HttpPost]
        [ValidateInput(false)]
        [ActionName("TopicHistory")]
        [AcceptSubmitButton(Name = "btnPreview", Value = "Preview")]
        [TopicActionFilter]
        public ActionResult btnPreview(FormCollection fc)
        {
            try {
                TopicVersion tv = new TopicVersion();
                tv.TopicName = fc[0];
                //tv.Version = fc["Version"] as Int32;
                tv.Body = fc[3];
                TopicVersionViewData tvd = new TopicVersionViewData(tv);
                // TODO: Add update logic here
                this.Session.Remove("__MyPreviewText");
                this.Session.Add("__MyPreviewText", tvd.Topic.HtmlText);
                return RedirectToAction("ShowPreview");
            }
            catch {
                return View();
            }
        }

        [HttpPost]
        [ValidateInput(false)]
        [ActionName("TopicHistory")]
        [AcceptSubmitButton(Name = "btnMakeCurrent", Value = "Make Current")]
        [TopicActionFilter]
        [MaliciousActivityChecksFilter]
        [HandleError(ExceptionType = typeof(UnauthorizedAccessException), View = "AccessRestricted")]
        public ActionResult btnMakeCurrent(FormCollection fc)
        {
            try {
                string pagePath = fc[2];
                WikiTopicRepository wtr = new WikiTopicRepository(Server.MapPath("~\\" + pagePath.Substring(0, pagePath.LastIndexOf("/"))));
                int curVersion = wtr.GetTopicCurrentVersionNumber(fc[0]);
                int getVersion;
                Int32.TryParse(fc[1], out getVersion);
                TopicVersion tv = wtr.GetSpecificVersion(fc[0], getVersion);
                tv.Version = curVersion + 1;
                if (Request.IsAuthenticated) {
                    tv.Creator = User.Identity.Name;
                }
                wtr.Add(tv);
                return RedirectToRoute("PageDisplay", new { action = "Page", controller = "WikiTopic", pagePath = pagePath });
            }
            catch {
                return RedirectToRoute("ShowAccessDenied", new { action = "ShowAccessDenied", controller = "WikiTopic", topicName = string.Empty });
            }
        }

        [HttpPost]
        [ValidateInput(false)]
        [ActionName("TopicHistory")]
        [AcceptSubmitButton(Name = "btnShowDiff", Value = "Show Difference")]
        [HandleError(ExceptionType = typeof(UnauthorizedAccessException), View = "AccessRestricted")]
        [TopicActionFilter]
        public ActionResult btnShowDiff(FormCollection fc)
        {
            try {
                string pagePath = fc[2];
                WikiTopicRepository wtr = new WikiTopicRepository(Server.MapPath("~\\" + pagePath.Substring(0, pagePath.LastIndexOf("/"))));
                int getFirstVersion;
                int getSecondVersion;
                Int32.TryParse(fc[1], out getFirstVersion);
                Int32.TryParse(fc[3], out getSecondVersion);
                TopicVersion tv1 = wtr.GetSpecificVersion(fc[0], getFirstVersion);
                TopicVersion tv2 = wtr.GetSpecificVersion(fc[0], getSecondVersion);
                string older;
                string newer;
                if (getSecondVersion > getFirstVersion) {
                    older = tv1.WikiText;
                    newer = tv2.WikiText;
                }
                else {
                    older = tv2.WikiText;
                    newer = tv1.WikiText;
                }

                //return RedirectToAction("ShowDiff", new { oldText = older, newText = newer });
                ISideBySideDiffBuilder diffBuilder = new SideBySideDiffBuilder(new Differ());
                SideBySideDiffModel model = diffBuilder.BuildDiffModel(older ?? string.Empty, newer ?? string.Empty);

                return View("ShowDiff", model);
            }
            catch {
                return RedirectToRoute("ShowAccessDenied", new { action = "ShowAccessDenied", controller = "WikiTopic", topicName = string.Empty });
            }
        }

        [TopicActionFilter]
        public ActionResult ShowDiff(string oldText, string newText)
        {
            ISideBySideDiffBuilder diffBuilder = new SideBySideDiffBuilder(new Differ());
            SideBySideDiffModel model = diffBuilder.BuildDiffModel(oldText ?? string.Empty, newText ?? string.Empty);

            return View(model);
        }


        [TopicActionFilter]
        public ActionResult ShowPreview()
        {
            ViewBag.PreviewText = this.Session["__MyPreviewText"];
            return View("Preview");
        }

        [TopicActionFilter]
        public ActionResult ShowAccessDenied(string topicName)
        {
            ViewBag.TopicName = topicName;
            return View("AccessDenied");
        }

        //
        // GET: /WikiTopic/Delete/5
        [Authorize(Roles = "WikiAdmin")]
        [TopicActionFilter]
        public ActionResult Delete(string topicName, string wikiDir)
        {
            try {
                WikiTopicRepository wtr = new WikiTopicRepository(Server.MapPath("~\\" + wikiDir.Replace("/", @"\")));
                wtr.RemoveWikiTopic(topicName);

                WikiDiscussRepository wdr = new WikiDiscussRepository(Server.MapPath("~\\" + wikiDir.Replace("/", @"\")));
                wdr.RemoveWikiTopic(topicName);

                return RedirectToAction("WikiTopicListing", "WikiDirectory");
            }
            catch {
                return View();
            }
        }

        //
        // POST: /WikiTopic/Delete/5

        [HttpPost]
        [Authorize(Roles = "WikiAdmin")]
        [TopicActionFilter]
        public ActionResult DeletePost(string topicName, string wikiDir)
        {
            try
            {
                WikiTopicRepository wtr = new WikiTopicRepository(Server.MapPath("~\\" + wikiDir.Replace("/", @"\")));
                wtr.RemoveWikiTopic(topicName);

                return RedirectToAction("WikiTopicListing", "WikiDirectory");
            }
            catch
            {
                return View();
            }
        }

        private bool DenyRead(TopicVersion tv)
        {
            bool testRead = false;
            if (Request.IsAuthenticated) {
                testRead = TopicAccess.DenyRead(User, tv, null);
            }
            else {
                testRead = TopicAccess.DenyRead(null, tv, null);
            }
            return testRead;
        }

        private List<MenuTab> BuildMenuTabs()
        {
            return new List<MenuTab> {
                MenuTab.Create("Topic", "Index", null, "WikiTopic"),
                MenuTab.Create("Discussion", "Index", null, "TopicDiscussion"),
                MenuTab.Create("Read", "Index", null, "WikiTopic"),
                MenuTab.Create("Edit", "Edit", null, "WikiTopic"),
                MenuTab.Create("History", "History", null, "WikiTopic")
            };
        }
        private List<MenuTab> BuildMenuTabs(string pagePath)
        {
            return new List<MenuTab> {
                MenuTab.Create("Topic", "Page", pagePath, "WikiTopic"),
                MenuTab.Create("Discussion", "Page", pagePath, "TopicDiscussion"),
                MenuTab.Create("Read", "Page", pagePath, "WikiTopic"),
                MenuTab.Create("Edit", "Edit", pagePath, "WikiTopic"),
                MenuTab.Create("History", "History", pagePath, "WikiTopic")
            };
        }

        public ActionResult CaptchaImage(string prefix, bool noisy = true)
        {
            Random rand = new Random((int)DateTime.Now.Ticks);

            int a = rand.Next(10, 99);
            int b = rand.Next(1, 20);
            string captcha = string.Format("{0} + {1} = ?", a, b);

            Session["Captcha" + prefix] = a + b;

            FileContentResult img = null;

            using (MemoryStream mem = new MemoryStream()) {
                using (Bitmap bmp = new Bitmap(130, 30)) {
                    using (Graphics gfx = Graphics.FromImage((Image)bmp)) {
                        gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
                        gfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                        gfx.FillRectangle(Brushes.White, new Rectangle(0, 0, bmp.Width, bmp.Height));

                        if (noisy) {
                            int i, r, x, y;
                            Pen pen = new Pen(Color.Yellow);
                            for (i = 1; i < 10; i++) {
                                pen.Color = Color.FromArgb(rand.Next(0, 255), rand.Next(0, 255), rand.Next(0, 255));
                                r = rand.Next(0, (130 / 3));
                                x = rand.Next(0, 130);
                                y = rand.Next(0, 30);

                                gfx.DrawEllipse(pen, x - r, y - r, r, r);
                            }
                        }
                        gfx.DrawString(captcha, new Font("Tahoma", 15), Brushes.Gray, 2, 3);

                        bmp.Save(mem, System.Drawing.Imaging.ImageFormat.Jpeg);
                        img = this.File(mem.GetBuffer(), "image/Jpeg");
                    }
                }
            }

            return img;
        }

        [MaliciousActivityChecksFilter]
        [HandleError(ExceptionType = typeof(UnauthorizedAccessException), View = "AccessRestricted")]
        public ActionResult WikiTopicLockAction(string topicName, string wikiDir, string pagePath, string lockAction)
        {
            WikiDirectoryRepository wdr = new WikiDirectoryRepository(Server.MapPath(@"~\" + wikiDir));
            wdr.WikiTopicLockAction(topicName, lockAction);

            return RedirectToRoute("PageDisplay", new { action = "Page", controller = "WikiTopic", pagePath });
        }

        [TopicActionFilter]
        public ActionResult TopicRename(string topicName, string pagePath)
        {
            TopicRenameViewData trd = new TopicRenameViewData(topicName, pagePath);
            return View(trd);
        }

        [HttpPost]
        [MaliciousActivityChecksFilter]
        [HandleError(ExceptionType = typeof(UnauthorizedAccessException), View = "AccessRestricted")]
        public ActionResult TopicRename(TopicRenameViewData trd)
        {
            try {
                if (Request.Form.Keys[1] == "btnCancel") {
                    return RedirectToRoute("PageDisplay", new { action = "Page", controller = "WikiTopic", pagePath = trd.PagePath });
                }
                else {
                    WikiTopicRepository wtr = new WikiTopicRepository(Server.MapPath("~\\" + trd.PagePath.Substring(0, trd.PagePath.LastIndexOf("/"))));
                    bool rtnValue = wtr.RenameTopic(trd.TopicName, trd.NewTopicName);
                    if (!rtnValue) {
                        trd.ErrorMsg = "There was an error during the rename process";
                        return View(trd);
                    }
                    string pagePath = trd.PagePath.Substring(0, trd.PagePath.LastIndexOf("/")) + "/" + trd.NewTopicName;
                    return RedirectToRoute("PageDisplay", new { action = "Page", controller = "WikiTopic", pagePath });
                }
            }
            catch {
                return RedirectToRoute("ShowAccessDenied", new { action = "ShowAccessDenied", controller = "WikiTopic", topicName = trd.TopicName });
            }
        }

        [MaliciousActivityChecksFilter]
        [TopicActionFilter]
        public ActionResult TopicInboundLinks(string topicName, string pagePath)
        {
            try {
                ViewBag.InboundTopicName = topicName;
                pagePath = "/" + pagePath.Substring(0, pagePath.LastIndexOf("/"));
                List<SearchData> sdl = new List<SearchData>();
                List<WikiTopicListingViewData> wtl = new List<WikiTopicListingViewData>();
                sdl = LuceneSearch.Search(topicName).ToList();
                foreach (SearchData item in sdl) {
                    WikiTopicRepository wtr = new WikiTopicRepository(Server.MapPath("~\\" + item.WikiDir));
                    TopicVersion tv = wtr.GetCurrentVersion(item.TopicName);
                    if (tv.Body.Contains("<StartsWithOneCap>" + topicName + "</StartsWithOneCap><Namespace>" + pagePath + "</Namespace>")) {
                        WikiTopicListingViewData wt = new WikiTopicListingViewData();
                        wt.TopicName = item.TopicName;
                        wt.WikiDir = item.WikiDir;
                        wt.CreatedOn = tv.CreatedOn;
                        wtl.Add(wt);
                    }
                }
                return View(wtl);
            }
            catch {
                return RedirectToRoute("ShowAccessDenied", new { action = "ShowAccessDenied", controller = "WikiTopic", topicName = topicName });
            }
        }



    }
}
